package com.gin.no_gay_alliance.fragment.notice;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.FragmentNoticeTab2Binding;
import com.gin.no_gay_alliance.enums.MessageWhat;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.utils.ViewUtils;
import com.gin.no_gay_alliance.viewmodel.notice.BaseNoticeModel;

import java.util.ArrayList;

/**
 * 提醒消息tab基类
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/10 00:09
 **/
public abstract class BaseNoticeTabFragment<B extends ViewBinding, D extends BaseNoticeModel<?>> extends BaseBindingFragment<FragmentNoticeTab2Binding> implements View.OnLongClickListener {
    protected static final String ARG_KEY = "DATA";
    protected ArrayList<D> data;
    protected BaseBindingRecyclerViewAdapter<B, D> adapter;

    protected abstract B buildBinding(LayoutInflater inflater);

    /**
     * 绑定数据
     */
    protected abstract void onBindViewHolder(BaseBindingRecyclerViewAdapter.ViewHolder<B> holder, @NonNull B b, int position, D item);

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        adapter = new BaseBindingRecyclerViewAdapter<B, D>(requireContext(), data) {
            @Override
            protected void onBindViewHolder(ViewHolder<B> holder, @NonNull B b, int position, D item) {
                BaseNoticeTabFragment.this.onBindViewHolder(holder, b, position, item);

                // 绑定点击事件
                b.getRoot().setTag(item);
                b.getRoot().setOnClickListener(BaseNoticeTabFragment.this);
                b.getRoot().setOnLongClickListener(BaseNoticeTabFragment.this);
            }

            @NonNull
            @Override
            protected B buildBinding(LayoutInflater inflater) {
                return BaseNoticeTabFragment.this.buildBinding(inflater);
            }
        };
        b.recyclerView.setAdapter(adapter);
        ViewUtils.setGone(!ObjectUtils.isEmpty(data), b.tvWarning);
    }

    @Override
    public FragmentNoticeTab2Binding createViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentNoticeTab2Binding.inflate(inflater, container, false);
    }

    /**
     * 获取和处理参数
     *
     * @param arguments 参数
     */
    @Override
    protected void handleArguments(Bundle arguments) {
        if (arguments != null && arguments.getParcelableArrayList(ARG_KEY) != null) {
            data = arguments.getParcelableArrayList(ARG_KEY);
        } else {
            throw new RequireParamException(ARG_KEY);
        }
    }

    /**
     * 点击事件统一处理
     *
     * @param v 被点击的View
     */
    @Override
    public void onClick(View v) {
        // 已读
        read(v);
    }

    protected void read(View v) {
        D item = (D) v.getTag();
        // 设置已读
        item.read = true;
        // 通知适配器更新
        adapter.notifyItemChanged(data.indexOf(item));
        // 通知父级更新和保存
        getParentHandler().sendEmptyMessage(MessageWhat.ON_READ_NOTICE);
    }

    /**
     * 长按事件
     *
     * @param v 被点击的item
     * @return true if the callback consumed the long click, false otherwise.
     */
    @Override
    public boolean onLongClick(View v) {
        // 已读
        read(v);
        return false;
    }

    /**
     * 设置数据
     *
     * @param data 数据
     */
    public void setData(ArrayList<D> data) {
        data = !ObjectUtils.isEmpty(data) ? data : new ArrayList<>();
        Bundle arguments = getArguments();
        arguments = arguments != null ? arguments : new Bundle();
        arguments.putParcelableArrayList(ARG_KEY, data);
        setArguments(arguments);

        // 如果适配器已存在，更新数据
        if (adapter != null) {
            adapter.updateData(data);
        }
    }
}
